package ca.clarity.hms.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Community Volunteer 목록을 출력하는 activity class 이다.
 *
 * @author Junghun, Shin
 * @version 1.0
 */
public class HmsCvListActivity extends Activity {

    private static String TAG = HmsCvListActivity.class.getSimpleName();

    private ProgressDialog progressDialog;

    JSONArray communityVolunteers;

    ArrayList<HashMap<String, String>> cvList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_hms_cv_list);


        Log.d(TAG, "네트워크가 활성화 상태입니다.");


        cvList = new ArrayList<HashMap<String, String>>();

        //new CommunityVolunteers().execute();


        // Call communityVolunteers webservice
        String communityVolunteerStr = "{\n" +
                "    \"d\": {\n" +
                "        \"result\": \"success\",\n" +
                "        \"data\": [\n" +
                "            {\n" +
                "                \"username\": \"shin 1\",\n" +
                "                \"password\": \"aaaa\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"username\": \"shin 2\",\n" +
                "                \"password\": \"bbbb\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"username\": \"shin 3\",\n" +
                "                \"password\": \"cccc\"\n" +
                "            }\n" +
                "        ]\n" +
                "    }\n" +
                "}";

        ArrayAdapter<String> cvAdapter = null;


        try {
            JSONObject jsonObject = new JSONObject(communityVolunteerStr);

            communityVolunteers = jsonObject.getJSONObject("d").getJSONArray("data");

            /**
             * 환자 목록
             */
            cvAdapter = new ArrayAdapter<String>(this, R.layout.simple_list_item_3);

            for (int i = 0; i < communityVolunteers.length(); i++) {

                JSONObject c = communityVolunteers.getJSONObject(i);

                Log.d("CV : ", c.getString("username"));

                cvAdapter.add(c.getString("username"));
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

        // ListView 가져오기
        ListView cvListView = (ListView) findViewById(R.id.cvListView);

        // ListView에 각각의 아이템표시를 제어하는 Adapter를 설정
        cvListView.setAdapter(cvAdapter);

        // 아이템을 [클릭]시의 이벤트 리스너를 등록
        cvListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ListView listView = (ListView) parent;

                Log.i(TAG, "아이템을 [클릭]시의 이벤트 리스너를 등록");

                // Login 페이지로 이동
                Intent intent = new Intent(HmsCvListActivity.this, HmsLoginActivity.class);
                startActivity(intent);
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        //getMenuInflater().inflate(R.menu.hms_village, menu);
        return true;
    }
}
